package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.mapper.UserDao;
import com.example.demo.entity.UserEntity;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.RespCode;
import com.example.demo.service.UserService;
import com.example.demo.utils.JwtUtils;
import com.example.demo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private JwtUtils jwtUtils;


    @Override
    public UserEntity register(UserVo userVo) {
        //todo 需要验证码。。。。。

        if (StringUtils.isEmpty(userVo.getUsername())) {
            throw new BusinessException(RespCode.PARAMETER_ERROR.getName());
        }
        if (StringUtils.isEmpty(userVo.getPassword())) {
            throw new BusinessException(RespCode.PARAMETER_ERROR.getName());
        }
        LambdaQueryWrapper<UserEntity> userEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userEntityLambdaQueryWrapper.eq(UserEntity::getUsername, userVo.getUsername());
        UserEntity userEntity = userDao.selectOne(userEntityLambdaQueryWrapper);
        //判决用户是否存在了
        if (!Objects.isNull(userEntity)) {
            throw new BusinessException(RespCode.REGISTER_ERROR_USER_EXIT.getName());
        }
        //加密密码
        String encryptPassword = jwtUtils.encryptPassword(userVo.getPassword());
        UserEntity user = new UserEntity();
        user.setPassword(encryptPassword);
        user.setUsername(userVo.getUsername());
        user.setStatus("0");
        int insert = userDao.insert(user);
        UserEntity result = userDao.selectOne(userEntityLambdaQueryWrapper);
        return result;


    }

    @Override
    public String login(UserVo userVo) {
        //查询通过用户名
        LambdaQueryWrapper<UserEntity> userEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userEntityLambdaQueryWrapper.eq(UserEntity::getUsername, userVo.getUsername());
        UserEntity userEntity = userDao.selectOne(userEntityLambdaQueryWrapper);
        if (Objects.isNull(userEntity)) {
            throw new BusinessException(RespCode.REGISTER_ERROR_USERNAME.getName());
        }
        Boolean loginSuccess = jwtUtils.checkPassword(userVo.getPassword(), userEntity.getPassword()); // 对用户输入的密码进行哈希处理


        if (loginSuccess) {
            //登录成功，生成token
            String token = jwtUtils.createToken(userVo.getUsername(), userEntity.getId(), "来源1", 60 * 24);
            return token;
        } else {
            throw new BusinessException(RespCode.REGISTER_ERROR_PASSWORD.getName());
        }


    }

    @Override
    public List<UserEntity> findAllUser() {
        return userDao.findAll();
    }


}
